<template>
    <div>
        <el-dialog :title="title" :visible.sync="addVisible" :close-on-click-modal="false"
            :close-on-press-escape="false" @closed="handlerCancel()">
            <el-form :model="roleInfo">
                <el-form-item label="id" :label-width="formLabelWidth" v-if="!isadd">
                    <el-input v-model="roleInfo.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="角色名称" :label-width="formLabelWidth">
                    <el-input v-model="roleInfo.roleName"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="handlerCancel()">取 消</el-button>
                <el-button type="primary" @click="handlerSave()">保 存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { roleInfByIdoApi,addRoleApi,editRoleApi } from    '../../api/RoleApi'
export default {
    props: ['addVisible', "title"],
    data() {
        return {
            roleInfo: {
                id: 0,
                roleName: "",
            },
            isadd: false,
            formLabelWidth: '120px',
        };
    },
    methods: {
        handlerCancel() {
            this.$emit('closeDialog');

        },
       async handlerSave() {
            if (this.isadd) {
                try {
                    const res = await addRoleApi(this.roleInfo);
                    if (res.status === 200) {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.$emit('closeDialog',true);
                            }
                        });
          
                    } else {
                        this.$message.error(res.msg);
                    }
                } catch (error) {
                    console.error('获取用户列表失败:', error);
                    this.$message.error('网络错误，请稍后重试');
                }

            }else{
                try {
                    const res = await editRoleApi(this.roleInfo);
                    if (res.status === 200) {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.$emit('closeDialog',true);
                            }
                        });
          
                    } else {
                        this.$message.error(res.msg);
                    }
                } catch (error) {
                    console.error('获取用户列表失败:', error);
                    this.$message.error('网络错误，请稍后重试');
                }

            }
            
        },
        
        async openDialog(roleId) {
            if (!roleId) {
                this.roleInfo = {
                    id: 0,
                    roleName: "",
                }
                this.isadd = true;
            } else {
                try {
                    this.isadd = false;
                    const res = await roleInfByIdoApi(roleId);
                    if (res.status === 200) {
                        this.roleInfo = res.data;
                    } else {
                        this.$message.error(res.msg);
                    }
                } catch (error) {
                    console.error('获取用户列表失败:', error);
                    this.$message.error('网络错误，请稍后重试');
                }
                

            }
        },
       

    }
};

</script>
<style scoped lang="less">
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>